<?php
//$Id: yahoo.class.php 24762 2012-09-03 14:21:04Z xuwh $
class ThirdLogin_Yahoo
{
	private $cfg_auth_url;
	private $cfg_realm_url;
	private $cfg_policy_url;
	
	function __construct(){
		func::start_session();
		//FIXME::测试上.
		$domain		=	COOKIE_DOMAIN;
		$this->cfg_auth_url   =   "http://www{$domain}/account/connect/yahoo";
		$this->cfg_realm_url  =   "http://www{$domain}/account/connect/yahoo";
		$this->cfg_policy_url =   "http://www{$domain}/account/connect/yahoo";	
	}
	
	public function set_url($urlArr){
		$this->cfg_auth_url   =   @$urlArr['auth_url'];
		$this->cfg_realm_url  =   @$urlArr['realm_url'];
		$this->cfg_policy_url =   @$urlArr['policy_url'];
	}
	//如果没有登录，则获取登录信息.
	public function get_login_url($redirect=false)
	{
		//$this->cfg_auth_url	=	$redirect ? $redirect : $this->cfg_auth_url;
		$openid_server = "https://open.login.yahooapis.com/openid/op/auth";
	    $data['openid.ns'] = 'http://specs.openid.net/auth/2.0';
	    $data['openid.mode'] = 'associate';
	    $data['openid.assoc_type'] = 'HMAC-SHA1';
	    $data['openid.session_type'] = 'no-encryption';
	    $AssocHandle = $this->getAssociationHandle($openid_server.'?'.http_build_query($data));
	    $_SESSION['yahoo_handle']	=	$AssocHandle;
	    unset($data);
	    
	    
	    $data['openid.assoc_handle'] = $AssocHandle;
	    $data['openid.ax.mode'] = 'fetch_request';
	    
	    $data['openid.ax.required'] = 'attr1';
	    $data['openid.ax.type.attr1'] = 'http://axschema.org/contact/email';
	    
	    $data['openid.claimed_id'] = 'http://specs.openid.net/auth/2.0/identifier_select';
	    $data['openid.identity'] = 'http://specs.openid.net/auth/2.0/identifier_select';
	    $data['openid.mode'] = 'checkid_setup';
	    $data['openid.ns'] = 'http://specs.openid.net/auth/2.0';
	    $data['openid.ns.ax'] = 'http://openid.net/srv/ax/1.0';
	    $data['openid.ns.sreg'] = 'http://openid.net/extensions/sreg/1.1';
	    $data['openid.realm'] = $this->cfg_realm_url;
	    $data['openid.return_to'] = $this->cfg_auth_url;
	    //$data['openid.sreg.optional'] = 'nickname,email';
	    $data['openid.sreg.policy_url'] = $this->cfg_policy_url;   //TODO
	    //$data['openid.sreg.required'] = 'email';
	    $data['openid.trust_root'] = $this->cfg_realm_url;         //TODO
	
	    $data['xopenid_lang_pref'] = 'tw';
	
		echo 'loading to Yahoo ...';
	    
	    //生成跳轉URL
	    $LocationUrl  = $openid_server.'?'.http_build_query($data);
	    return $LocationUrl;
	}
	
	//判断是否登录成功，回传.
	function is_callback_url(){
		if(empty($_GET['openid_mode'])){
			return false;
		}else{
			$AssocHandle = $_SESSION['yahoo_handle'];
		    if('id_res' == $_GET['openid_mode']
		    && $AssocHandle == $_GET['openid_assoc_handle']) {
		    	return true;
		    }else{
		    	return false;
		    }
		}
	}
	//获取email
	function get_user(){
		if($this->is_callback_url()){
			return array('email'	=>	$_GET['openid_ax_value_email']);
		}
		return false;
	}
	
	function getAssociationHandle($url) {
	    $c = curl_init($url);
	    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($c, CURLOPT_HEADER, false);
	    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
	    $contents = curl_exec($c);
	    curl_close($c);
	    $assoc_handle = time();
	    $lines = explode("\n", $contents);
	    foreach($lines as $line){
	     	if(substr($line, 0, 13) == "assoc_handle:"){
		      	$assoc_handle = substr($line, 13);
		      	break;
	     	}
	    }
	    return $assoc_handle;
	}
}